import type { AxiosResponse } from "axios";
import axios from "./index";
import type { Comment } from "./types";

// 添加评论
export async function addComment(
  userId: number,
  productId: number,
  content: string,
  rate: number
): Promise<{ success: boolean; message: string; commentId?: number }> {
  try {
    const response: AxiosResponse<{
      success: boolean;
      message: string;
      commentId?: number;
    }> = await axios.post("/comments", {
      userId,
      productId,
      content,
      rate,
    });
    return response.data;
  } catch (error: any) {
    throw new Error(error.response?.data.message || "添加评论失败");
  }
}

// 获取某个产品的评论列表
export async function getCommentsByProductId(
  productId: number
): Promise<{ success: boolean; comments: Comment[] }> {
  try {
    const response: AxiosResponse<{ success: boolean; comments: Comment[] }> =
      await axios.get(`/comments/product/${productId}`);
    return response.data;
  } catch (error: any) {
    throw new Error(error.response?.data.message || "获取评论失败");
  }
}
